package com.ruoyi.web.controller.demo.controller;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;













import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;












import com.alibaba.fastjson.JSON;
import com.ruoyi.Goods.service.IGoodsService;
import com.ruoyi.GoodsAttach.domain.GoodsAttach;
import com.ruoyi.GoodsAttach.service.IGoodsAttachService;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;



/**
 * 表单相关
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/demo/form")
public class DemoFormController extends BaseController
{
    private String prefix = "demo/form";
    
    @Autowired
	private IGoodsAttachService goodsAttachService;

    private final static List<UserFormModel> users = new ArrayList<UserFormModel>();
    {
        users.add(new UserFormModel(1, "1000001", "测试1", "15888888888"));
        users.add(new UserFormModel(2, "1000002", "测试2", "15666666666"));
        users.add(new UserFormModel(3, "1000003", "测试3", "15666666666"));
        users.add(new UserFormModel(4, "1000004", "测试4", "15666666666"));
        users.add(new UserFormModel(5, "1000005", "测试5", "15666666666"));
    }

    /**
     * 按钮页
     */
    @GetMapping("/button")
    public String button()
    {
        return prefix + "/button";
    }

    /**
     * 下拉框
     */
    @GetMapping("/select")
    public String select()
    {
        return prefix + "/select";
    }

    /**
     * 表单校验
     */
    @GetMapping("/validate")
    public String validate()
    {
        return prefix + "/validate";
    }

    /**
     * 功能扩展（包含文件上传）
     */
    @GetMapping("/jasny")
    public String jasny()
    {
        return prefix + "/jasny";
    }

    /**
     * 拖动排序
     */
    @GetMapping("/sortable")
    public String sortable()
    {
        return prefix + "/sortable";
    }

    /**
     * 选项卡 & 面板
     */
    @GetMapping("/tabs_panels")
    public String tabs_panels()
    {
        return prefix + "/tabs_panels";
    }

    /**
     * 栅格
     */
    @GetMapping("/grid")
    public String grid()
    {
        return prefix + "/grid";
    }

    /**
     * 表单向导
     */
    @GetMapping("/wizard")
    public String wizard()
    {
        return prefix + "/wizard";
    }

    /**
     * 文件上传
     */
    @GetMapping("/upload")
    public String upload(ModelMap mmap)
    {
    	GoodsAttach goodsAttach=new GoodsAttach();
   	 	goodsAttach.setGoodsId(327);
   	 	List<GoodsAttach> list=goodsAttachService.selectGoodsAttachList(goodsAttach);
   	 	String previewListEdit=JSON.toJSON(list).toString();
   	 	mmap.put("previewListEdit",previewListEdit);
        return prefix + "/upload";
    }
    
//    @GetMapping("/uploadTest")
//    public String uploadTest(ModelMap mmap)
//    {
//    	 GoodsAttach goodsAttach=new GoodsAttach();
//    	 goodsAttach.setGoodsId(327);
//    	 List<GoodsAttach> goodsAttachList=goodsAttachService.selectGoodsAttachList(goodsAttach);
//    	 mmap.put("previewListEdit", goodsAttachList);
//         return prefix + "/upload";
//    }
    
	@PostMapping("/uploadTest/{goods_id}")
	@ResponseBody
	public AjaxResult addManySave(@RequestParam("file") MultipartFile[] files,
			@PathVariable("goods_id") String goods_id) throws IOException {
		for (int i = 0; i < files.length; i++) {
			MultipartFile file = files[i];
			if (file.isEmpty()) {
				return AjaxResult.error();
			} else {
				String fileName = file.getOriginalFilename();
				String filepath = "C:/ruoyi/";
				File localFile = new File(filepath);
				if (!localFile.exists()) {
					localFile.mkdirs();
				}
				String path = FileUploadUtils.upload(Global.getAvatarPath(),
						file);
				GoodsAttach goodsAttach = new GoodsAttach();
				goodsAttach.setUrl(path);
				goodsAttach.setFileName(fileName);
				goodsAttach.setGoodsId(327);
				goodsAttachService.insertGoodsAttach(goodsAttach);				
			}
		}
		AjaxResult ajax = new AjaxResult();
		ajax.put("code", 200);
		return ajax;
	}
	
	
	@PostMapping("/deleteTest")
	@ResponseBody
	public AjaxResult deleteSave(String key)
			throws IOException {
		Integer realKey=Integer.parseInt(key);
		GoodsAttach goodsAttach = goodsAttachService.selectGoodsAttachById(realKey);
		String fileName = goodsAttach.getUrl();	
		String Path =  "C:/" + fileName;
		File file = new File(Path);
		if (file.exists()) {
			FileUtils.deleteQuietly(file);
		}
		return toAjax(goodsAttachService.deleteGoodsAttachByIds(key));
		
		
	}
    /**
     * 日期和时间页
     */
    @GetMapping("/datetime")
    public String datetime()
    {
        return prefix + "/datetime";
    }

    /**
     * 左右互选组件
     */
    @GetMapping("/duallistbox")
    public String duallistbox()
    {
        return prefix + "/duallistbox";
    }

    /**
     * 基本表单
     */
    @GetMapping("/basic")
    public String basic()
    {
        return prefix + "/basic";
    }

    /**
     * 搜索自动补全
     */
    @GetMapping("/autocomplete")
    public String autocomplete()
    {
        return prefix + "/autocomplete";
    }

    /**
     * 获取用户数据
     */
    @GetMapping("/userModel")
    @ResponseBody
    public AjaxResult userModel()
    {
        AjaxResult ajax = new AjaxResult();

        ajax.put("code", 200);
        ajax.put("value", users);
        return ajax;
    }

    /**
     * 获取数据集合
     */
    @GetMapping("/collection")
    @ResponseBody
    public AjaxResult collection()
    {
        String[] array = { "ruoyi 1", "ruoyi 2", "ruoyi 3", "ruoyi 4", "ruoyi 5" };
        AjaxResult ajax = new AjaxResult();
        ajax.put("value", array);
        return ajax;
    }
}

class UserFormModel
{
    /** 用户ID */
    private int userId;

    /** 用户编号 */
    private String userCode;

    /** 用户姓名 */
    private String userName;

    /** 用户手机 */
    private String userPhone;

    public UserFormModel()
    {

    }

    public UserFormModel(int userId, String userCode, String userName, String userPhone)
    {
        this.userId = userId;
        this.userCode = userCode;
        this.userName = userName;
        this.userPhone = userPhone;
    }

    public int getUserId()
    {
        return userId;
    }

    public void setUserId(int userId)
    {
        this.userId = userId;
    }

    public String getUserCode()
    {
        return userCode;
    }

    public void setUserCode(String userCode)
    {
        this.userCode = userCode;
    }

    public String getUserName()
    {
        return userName;
    }

    public void setUserName(String userName)
    {
        this.userName = userName;
    }

    public String getUserPhone()
    {
        return userPhone;
    }

    public void setUserPhone(String userPhone)
    {
        this.userPhone = userPhone;
    }

}
